﻿<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .wrap {
            width: 400px;
            height: 400px;
            margin: 100px auto 0;
        }

        .wrap h1 {
            text-align: center;
        }

        .wrap div {
            width: 400px;
            height: 300px;
            background: pink;
            font-size: 30px;
            text-align: center;
            line-height: 300px;
        }
    </style>
    <script src="./jquery-1.11.1.js"></script>
</head>

<body>

    <div class="wrap">
        <h1>按键改变颜色</h1>
        <div id="bgChange">
            keyCode为：
            <span id="keyCodeSpan"></span>
        </div>
    </div>
    <script>
        $(document).on('keydown', function (event) {
            console.log(event.keyCode);

            if (event.keyCode === 13) { // Enter
                $('#bgChange').css('background', 'red')
            } else if (event.keyCode === 222) {//''
                $('#bgChange').css('background', '#1e1e1e')
            } else if (event.keyCode === 186) {//;
                $('#bgChange').css('background', '#73c991')
            } else if (event.keyCode === 76) {//l
                $('#bgChange').css('background', '#fff')
            } else if (event.keyCode === 75) {//k
                $('#bgChange').css('background', '#007acc')
            } else if (event.keyCode === 74) {//j
                $('#bgChange').css('background', '#90e7cd')
            } else if (event.keyCode === 72) {//h
                $('#bgChange').css('background', '#fcb397')
            }
            $('#keyCodeSpan').text(event.keyCode)
        })
    </script>
</body>

</html>